function populateSelect(xml, rootItem, callback) {
    var firstItem = true;
    $(xml).find("*").each(function () {
        var tagName = firstItem ? "" : this.tagName;
        firstItem = false;
        var parent = this.parentElement;
        while (parent != null && parent.tagName != rootItem) {
            tagName = parent.tagName + "->" + tagName;
            parent = parent.parentElement;
        }
        if (this.children.length == 0) {
            var option = $("#xmlFields").find("option[value='" + tagName + "']");
            if (option.length == 0) {
                $("#xmlFields").append($("<option value='" + tagName + "'>" + tagName + "</option>"));
            }
            option = $("#secondaryXmlFields").find("option[value='" + tagName + "']");
            if (option.length == 0) {
                $("#secondaryXmlFields").append($("<option value='" + tagName + "'>" + tagName + "</option>"));
            }
            if (callback != null) {
                callback(tagName, $("<option value='" + tagName + "'>" + tagName + "</option>"));
            }
        }
        for (var i = 0; i < this.attributes.length; i++) {
            var item = this.attributes[i];
            var value = tagName + "->" + item.name;
            if (tagName == "") {
                value = item.name;
            }
            value += "(attribute)";
            var option = $("#xmlFields").find("option[value='" + value + "']");
            if (option.length == 0) {
                $("#xmlFields").append($("<option value='" + value + "'>" + value + "</option>"));
            }
            option = $("#secondaryXmlFields").find("option[value='" + value + "']");
            if (option.length == 0) {
                $("#secondaryXmlFields").append($("<option value='" + value + "'>" + value + "</option>"));
            }
            if (callback != null) {
                callback(value, $("<option value='" + value + "'>" + value + "</option>"));
            }
        }
    });
}

var mappingKeySet = false;

function getFormattedData(values, secondaryTable) {
    var data = [];
    var numberOfFields = 7;
    var columns = [];
    var xmlFields = [];
    var actions = [];
    var defaultValues = [];
    var plainValues = [];
    var dependsOn = [];
    var xmlTagIndex = [];
    var keyColumn = $("#keyColumn").val();
    var mappingColumn = $("#mappingColumn").val();
    mappingKeySet = false;

    var index = secondaryTable ? 1 : 2;
    for (var i = index; i < values.length; i += numberOfFields) {
        columns.push(values[i]);
        xmlFields.push(values[i + 1]);
        actions.push(values[i + 2]);
        defaultValues.push(values[i + 3]);
        plainValues.push(values[i + 4]);
        dependsOn.push(values[i + 5]);
        xmlTagIndex.push(values[i + 6]);
    }

    for (var l = 0; l < columns.length; l++) {
        var array = new Object();
        var mapping = new Object();
        var type = "tag";
        var value = xmlFields[l];
        if (plainValues[l] != "") {
            type = "value";
            value = plainValues[l];
        }
        if (xmlTagIndex[l] != "") {
            mapping['index'] = xmlTagIndex[l];
        }
        var attributeIndex = value.indexOf("(attribute)");
        if (attributeIndex != -1) {
            type = "attribute";
            value = value.substr(0, attributeIndex);
        }
        mapping['type'] = type;
        mapping['value'] = value;
        mapping['action'] = actions[l];
        mapping['default'] = defaultValues[l];
        mapping['dependsOn'] = dependsOn[l];
        if (columns[l] == mappingColumn) {
            mapping['key'] = 1;
            mappingKeySet = true;
        } else {
            mapping['key'] = 0;
        }

        array[columns[l]] = mapping;
        data.push(array);
    }
    return data;
}

function getSecondaryTablesMappings() {
    var data = [];
    // get all the inputs into an array.
    var tableMappings = $("#secondaryTableMappingWrapper div[id*='secondaryMappingWrapper']");
    tableMappings.each(function (index) {
        var secondaryTable = $(this).find("#secondaryTable").val();
        var $secondaryInputs = $(this).find(":input").not(":button");
        var values = [];
        $secondaryInputs.each(function () {
            values.push($(this).val());
        });

        var secondaryTableMapping = new Object();
        secondaryTableMapping['table'] = secondaryTable;
        secondaryTableMapping['mapping'] = getFormattedData(values, true);
        data.push(secondaryTableMapping);
    });
    return data;
}